<?php

namespace App\Kuafu\WaiMai\Jobs;

use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Exceptions\WaiMaiException;
use App\Kuafu\WaiMai\Model\WaiMaiOrderIdShopIdMappingModel;
use App\Kuafu\WaiMai\Service\WaiMaiFinishFoodService;
use App\Kuafu\WaiMai\Service\WaiMaiOrderService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

/**
 * 外卖延迟任务
 * Class OrderPayPolling
 * @package App\Kuafu\Order\Jobs
 */
class WaiMaiDelayTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    const QUEUE_NAME = '{RMS-API-WAI-MAI-DELAY-TASK}';

    /**
     * @var string orderId
     */
    private $orderId;

    private $taskType;

    private $data;

    const TASK_TYPE_1 = 1; // 延迟N分钟自动出餐
    const TASK_TYPE_2 = 2; // 预约单提醒
    const TASK_TYPE_3 = 3; // 弹窗版 - 自动出餐MQTT通知客户端
    const TASK_TYPE_4 = 4; // 弹窗版预约单 - 自动备餐MQTT通知客户端
    const TASK_TYPE_5 = 5; // 兜底的强制自动出餐

    const RESERVED_NOTICE_1 = 1; // 1小时
    const RESERVED_NOTICE_2 = 2; // 半小时
    const RESERVED_NOTICE_3 = 3; // 15分钟
    const RESERVED_NOTICE_4 = 4; // 10分钟

    // 任务延迟时间明细
    const TASK_DELAY_TIME = [
        self::TASK_TYPE_1 => [
            0 => 1800, // 预约单 - 预约送达时间-30分钟
            1 => 720,  // 即时单 - 过12分钟
        ],
        self::TASK_TYPE_2 => [
            self::RESERVED_NOTICE_1 => 3600,
            self::RESERVED_NOTICE_2 => 1800,
            self::RESERVED_NOTICE_3 => 900,
            self::RESERVED_NOTICE_4 => 600,
        ],
    ];

    /**
     * Create a new job instance.
     */
    public function __construct(int $taskType, int $orderId, array $data = [])
    {
        $this->taskType = $taskType;
        $this->orderId  = $orderId;
        $this->data     = $data;

        Log::info("WaiMaiDelayTask in:", [
            'taskType' => $taskType,
            'orderId'  => $orderId,
            'data'     => $data,
        ]);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $shopId = WaiMaiOrderIdShopIdMappingModel::getShopId($this->orderId);
        if (!$shopId) {
            return;
        }

        try {
            switch ($this->taskType) {
                case self::TASK_TYPE_1:
                    WaiMaiOrderService::sFinishFoods($this->orderId, $shopId);
                    break;

                case self::TASK_TYPE_2:
                    WaiMaiOrderService::reserveNotice($this->orderId, $this->data['reservedType'], $shopId);
                    break;

                case self::TASK_TYPE_3:
                    $silentSwitch = $this->data['silentSwitch'] ?? WaiMaiDBEnum::SILENT_DEFAULT_SWITCH;
                    (new WaiMaiFinishFoodService($shopId))->doFinishPush($this->orderId, $silentSwitch);
                    break;

                case self::TASK_TYPE_4:
                    (new WaiMaiFinishFoodService($shopId))->doPreparePush($this->orderId);
                    break;

                case self::TASK_TYPE_5:
                    $silentSwitch = $this->data['silentSwitch'] ?? WaiMaiDBEnum::SILENT_DEFAULT_SWITCH;
                    (new WaiMaiFinishFoodService($shopId))->doLastForceFinishPush($this->orderId, $silentSwitch);
                    break;
            }
        } catch (WaiMaiException $e) {
            Log::info("WAI-MAI-TASK-FILTER: orderId: {$this->orderId} task_type: {$this->taskType} ".$e->getMessage());
        }
    }
}
